package com.up.api.control;

import com.up.api.annotion.Remoting;
import com.up.api.feign.CoreApi;
import com.up.api.produce.ProduceTest;
import com.up.api.remote.Interface1;
import com.up.api.service.A;
import com.up.api.service.MongoService;
import com.up.api.xianliu.MyTest;
import com.up.api.xianliu.SlidingWindow;
import com.up.api.xianliu.XianLiU;
import com.up.tools.annotation.EnableAutoTryCatch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author: liuchengjun
 * @title: TextControl
 * @projectName: 海联技术中台V2.0
 * @depart 软件事业部
 * @description:
 * @date: 2023/8/29/029 11:32 (星期二)
 * @since V2.0
 */
@RestController
@RequestMapping("/up/api")
@Validated
@EnableAutoTryCatch
public class TextControl {
//    @Autowired
//    private A a;
    private AtomicInteger sum1 = new AtomicInteger(0);
    private AtomicInteger sum2 = new AtomicInteger(0);
    @Autowired
    private XianLiU xianLiU;
    @Autowired
    private MyTest myTest;
    @Autowired
    private SlidingWindow slidingWindow;
    @Resource
    private CoreApi coreApi;

    @Resource
    private ProduceTest produceTest;

//    @Remoting
//    private Interface1 interface1;
    @Autowired
    private MongoService mongoService;

    @GetMapping("/getNacosConfig")
    public String get() {
       return coreApi.get();
    }

    @GetMapping("docek")
    public String verify() {
//        a.text();
//        produceTest.sendText();
        return "不用验证";
    }
    @GetMapping("/mongoText")
    public String mongoText() {
        mongoService.mongoAsyc();
        return "不用验证";
    }

    @GetMapping("/xianliu")
    public boolean xianliu() {
        boolean b = myTest.allowRequest();
        if(!b){
            throw new RuntimeException("s");
        }
        return true;
//        if(b){
//            sum2.incrementAndGet();
//            sum1.incrementAndGet();
//            return true;
//        }else {
//            //CompletableFuture.runAsync(()->run());
//            return false;
//        }
    }
    public void run(){
        for (;;) {
            boolean b = slidingWindow.allowRequest();
            if(b){
                sum1.incrementAndGet();
                break;
            }
        }
    }

    @GetMapping("/xianliuredis")
    public boolean xianliu1() {
        for (; ;) {
            boolean b = xianLiU.acquire("user1");
            if(b){
                return true;
            }
        }
    }

    @GetMapping("/sum")
    public String xianliu11() {
       return sum2+"----"+sum1;
    }



}
